package com.xfcycle.zebrabikegp.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.xfcycle.zebrabikegp.R;
import com.xfcycle.zebrabikegp.assist.BlueToothAssist;
import com.xfcycle.zebrabikegp.constant.ToastConstant;
import com.xfcycle.zebrabikegp.util.HandleDataByBLEUtil;
import com.xfcycle.zebrabikegp.util.HandleDataBySharePreferencesUtil;

public class BikeSettingActivity extends RootActivity {

    private static final String TAG = "BikeSettingActivity";

    private ImageView iv_bike_setting_back;

    private RelativeLayout rl_setting_gear_power_output;
    private RelativeLayout rl_setting_sensor_calibration;
    private RelativeLayout rl_adjust_bike_dormancy_time;
    private RelativeLayout rl_setting_bluetooth_remote_control;
    private RelativeLayout rl_setting_firmware_update;
    private RelativeLayout rl_setting_speed_limit;
    private RelativeLayout rl_setting_alarm;
    private RelativeLayout rl_setting_low_battery_protect;
    private RelativeLayout rl_setting_regeneration_brake;

    private HandleDataByBLEUtil handleDataByBLEUtil;
    private HandleDataBySharePreferencesUtil handleDataBySharePreferencesUtil;

    private BikeSettingHandleDataFromBleReceiver bikeSettingHandleDataFromBleReceiver;

    private boolean automaticHeadLightStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bs_home);

        initView();
        initViewListener();
        setViewContent();
    }

    @Override
    protected void onResume() {
        super.onResume();

        setViewContent();

        //注册广播
//        bikeSettingHandleDataFromBleReceiver = new BikeSettingHandleDataFromBleReceiver();
//        registBroadcastReceiverAtActivity(bikeSettingHandleDataFromBleReceiver, BluetoothDeviceConstant.GET_DATA_FROM_BLUETOOTH_BROADCAST_RECEIVER_ACTION);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //回收广播接收者
//        unregisterReceiver(bikeSettingHandleDataFromBleReceiver);
    }

    /***********************************************************************************************/
    /**                                 初始化控件对象 监听 控件内容方法                             **/
    /***********************************************************************************************/

    private void initView() {
        iv_bike_setting_back = (ImageView) findViewById(R.id.iv_setting_back);

        handleDataByBLEUtil = HandleDataByBLEUtil.getInstance(BikeSettingActivity.this, -1);
        handleDataBySharePreferencesUtil = HandleDataBySharePreferencesUtil.getInstance(this);

//        switch_auto_headLight = (Switch) findViewById(R.id.switch_auto_headLight);

        rl_setting_gear_power_output = (RelativeLayout) findViewById(R.id.rl_setting_gear_power_output);
        rl_setting_sensor_calibration = (RelativeLayout) findViewById(R.id.rl_setting_sensor_calibration);
        rl_adjust_bike_dormancy_time = (RelativeLayout) findViewById(R.id.rl_adjust_bike_dormancy_time);
        rl_setting_bluetooth_remote_control = (RelativeLayout) findViewById(R.id.rl_setting_bluetooth_remote_control);
        rl_setting_firmware_update = (RelativeLayout) findViewById(R.id.rl_setting_firmware_update);
        rl_setting_speed_limit = (RelativeLayout) findViewById(R.id.rl_setting_speed_limit);
        rl_setting_alarm = (RelativeLayout) findViewById(R.id.rl_setting_alarm);
        rl_setting_low_battery_protect = (RelativeLayout) findViewById(R.id.rl_setting_low_battery_protect);
        rl_setting_regeneration_brake = (RelativeLayout) findViewById(R.id.rl_setting_regeneration_brake);
    }

    private void initViewListener() {
        iv_bike_setting_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                closeActivity();
            }
        });

        //页面跳转
        rl_setting_gear_power_output.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activityChange(GEAR_POWER_OUTPUT_ACTIVITY, null, null);
            }
        });

        rl_setting_sensor_calibration.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activityChange(SENSOR_CALIBRATION_ACTIVITY, null, null);
            }
        });

        rl_adjust_bike_dormancy_time.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activityChange(ADJUST_BIKE_DORMANCY_ACTIVITY, null, null);
            }
        });

        rl_setting_bluetooth_remote_control.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activityChange(BLUETOOTH_KEY_ACTIVITY, null, null);
            }
        });

        rl_setting_speed_limit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activityChange(SPEED_LIMIT_ACTIVITY, null, null);
            }
        });

        rl_setting_alarm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activityChange(ALARM_ACTIVITY, null, null);
            }
        });

        rl_setting_low_battery_protect.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activityChange(LOW_BATTERY_LEVEL_FOLDBACK_ACTIVITY, null, null);
            }
        });

        rl_setting_regeneration_brake.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activityChange(REGENERATION_BRAKE_ACTIVITY, null, null);
            }
        });

        rl_setting_firmware_update.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(BikeSettingActivity.this.getApplicationContext(), ToastConstant.FIRMWARE_UPDATE, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setViewContent() {
        if(null != BlueToothAssist.connectDeviceName && BlueToothAssist.connectDeviceName.contains("vta")){
            //当前连接的是Volta
            rl_setting_sensor_calibration.setVisibility(View.VISIBLE);
            rl_setting_alarm.setVisibility(View.VISIBLE);
            rl_setting_low_battery_protect.setVisibility(View.VISIBLE);
        }else {
            rl_setting_sensor_calibration.setVisibility(View.GONE);
            rl_setting_alarm.setVisibility(View.GONE);
            rl_setting_low_battery_protect.setVisibility(View.GONE);
        }

        //设置初始状态是开启还是关闭
//        String switchState = handleDataBySharePreferencesUtil.getDateFromSharePre(APP_INFO_SP_NAME, "switchState");
//
//        if (null == switchState) {
//            Log.e(TAG, "setViewContent: switchState为空");
//            return;
//        }

//        if ("on".equals(switchState)) {
//            switch_auto_headLight.setChecked(true);
//        }
//
//        if ("off".equals(switchState)) {
//            switch_auto_headLight.setChecked(false);
//        }
    }


    /***********************************************************************************************/
    /**                                 自定义广播接收者类                                          **/
    /***********************************************************************************************/

    /**
     * 广播接收者类，负责接收从蓝牙发出的数据并对其进行处理
     */
    class BikeSettingHandleDataFromBleReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
//            String result = intent.getStringExtra("result");

            //修改自动开启大灯至初始状态 写入失败
//            if ("writeFailure".equals(result) || result.contains("OK+Set:100")) {
//
//                toast("Modify the Automatic headlight failure..", 500);
//                SystemClock.sleep(250);
//                switch_auto_headLight.setChecked(automaticHeadLightStatus);
//
//                return;
//            }

            //修改成功，将当前状态加入到sp中存储
//            Map<String, String> params = new HashMap<>();

//            if (switch_auto_headLight.isChecked()) {
//                params.put("switchState", "on");
//                handleDataBySharePreferencesUtil.putDateIntoSharePre(APP_INFO_SP_NAME, params);
//                return;
//            }
//
//            if (!switch_auto_headLight.isChecked()) {
//                params.put("switchState", "off");
//                handleDataBySharePreferencesUtil.putDateIntoSharePre(APP_INFO_SP_NAME, params);
//                return;
//            }

        }
    }
}
